package com.draven.admin.controller;

import com.draven.common.core.domain.AjaxResult;
import com.draven.common.entity.NoteCategory;
import com.draven.system.service.INoteCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 笔记分类Controller
 *
 * @author draven
 */
@Controller
@RequestMapping("/note/category")
public class NoteCategoryController {

    private String prefix = "category";

    @Autowired
    private INoteCategoryService noteCategoryService;

    @GetMapping()
    public String category() {
        return prefix + "/category";
    }

    /**
     * 查询分类列表
     */
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult list(NoteCategory category) {
        List<NoteCategory> list = noteCategoryService.selectCategoryList(category);
        return AjaxResult.success(list);
    }



    /**
     * 查询分类列表
     */
    @GetMapping("/listAll")
    @ResponseBody
    public AjaxResult listAll() {
        List<NoteCategory> list = noteCategoryService.listAll();
        return AjaxResult.success(list);
    }

    /**
     * 查询分类列表（包含笔记数量）
     */
    @GetMapping("/listWithCount")
    @ResponseBody
    public AjaxResult listWithCount() {
        List<NoteCategory> list = noteCategoryService.selectCategoryListWithCount();
        return AjaxResult.success(list);
    }

    /**
     * 新增分类
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 校验分类名称
     */
    @GetMapping("/checkUnique")
    @ResponseBody
    public AjaxResult checkUnique(@RequestParam String categoryName) {
        NoteCategory category = new NoteCategory();
        category.setCategoryName(categoryName);
        if (!noteCategoryService.checkCategoryNameUnique(category)) {
            return AjaxResult.error("新增分类'" + category.getCategoryName() + "'失败，分类名称已存在");
        }
        return AjaxResult.success();
    }

    @GetMapping("/selectById")
    @ResponseBody
    public AjaxResult selectById(@RequestParam Long id) {
        return AjaxResult.success(noteCategoryService.selectById(id));
    }


    /**
     * 新增保存分类
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated @RequestBody NoteCategory category) {
        if (!noteCategoryService.checkCategoryNameUnique(category)) {
            return AjaxResult.error("新增分类'" + category.getCategoryName() + "'失败，分类名称已存在");
        }
        return AjaxResult.success(noteCategoryService.insertCategory(category));
    }

    /**
     * 修改分类
     */
    @GetMapping("/edit/{categoryId}")
    public String edit(@PathVariable("categoryId") Long categoryId, ModelMap mmap) {
        NoteCategory category = noteCategoryService.selectCategoryById(categoryId);
        mmap.put("category", category);
        return prefix + "/edit";
    }

    /**
     * 修改保存分类
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated @RequestBody NoteCategory category) {
        if (!noteCategoryService.checkCategoryNameUnique(category)) {
            return AjaxResult.error("修改分类'" + category.getCategoryName() + "'失败，分类名称已存在");
        }
        return AjaxResult.success(noteCategoryService.updateCategory(category));
    }

    /**
     * 删除分类
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return AjaxResult.success(noteCategoryService.deleteCategoryByIds(convertToLongArray(ids)));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 校验分类名称
     */
    @PostMapping("/checkCategoryNameUnique")
    @ResponseBody
    public boolean checkCategoryNameUnique(NoteCategory category) {
        return noteCategoryService.checkCategoryNameUnique(category);
    }

    /**
     * 转换字符串数组为Long数组
     */
    private Long[] convertToLongArray(String str) {
        String[] arr = str.split(",");
        Long[] longArr = new Long[arr.length];
        for (int i = 0; i < arr.length; i++) {
            longArr[i] = Long.parseLong(arr[i]);
        }
        return longArr;
    }
}
